package co.yixiang.modules.delay;

import co.yixiang.modules.auction.domain.YxAuctionOrder;
import co.yixiang.modules.auction.domain.YxAuctionRecord;
import co.yixiang.modules.auction.service.mapper.YxAuctionOrderMapper;
import co.yixiang.modules.auction.service.mapper.YxAuctionRecordMapper;
import co.yixiang.modules.commom.domain.YxAppdict;
import co.yixiang.modules.commom.service.YxAppdictService;
import co.yixiang.modules.commom.service.mapper.YxAppdictMapper;
import co.yixiang.modules.order.domain.YxStoreOrder;
import co.yixiang.modules.order.service.mapper.StoreOrderMapper;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 继承Application接口后项目启动时会按照执行顺序执行run方法
 * 通过设置Order的value来指定执行的顺序
 */
@Component
public class StartService implements ApplicationRunner {

    @Resource
    private RedisUtils redisUtil;
    @Resource
    private YxAuctionOrderMapper yxAuctionOrderMapper;
    @Resource
    private YxAuctionRecordMapper yxAuctionRecordMapper;
    @Resource
    private StoreOrderMapper storeOrderMapper;
    @Autowired
    private YxAppdictMapper yxAppdictMapper;
    @Autowired
    private YxUserService userService;
    @Autowired
    private YxAppdictService appdictService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(ApplicationArguments args) throws Exception {
//        YxAppdict pmfksj=yxAppdictMapper.findAppdictByCode("pmfksj");
//        YxAppdict spfksj= yxAppdictMapper.findAppdictByCode("spfksj");
//        List<YxAuctionRecord> yxAuctionRecords = yxAuctionRecordMapper.selectList(new QueryWrapper<YxAuctionRecord>().lambda().eq(YxAuctionRecord::getIsBuy, 0));
//        for (YxAuctionRecord yxAuctionRecord:yxAuctionRecords
//        ) {
//            redisUtil.set("paimai"+yxAuctionRecord.getId(),yxAuctionRecord.getId());
//        }
//        Integer paimaiTime=60;
//        Integer productTime=30;
//        if(null !=pmfksj){
//            paimaiTime=Integer.parseInt(pmfksj.getMemo());
//        }
//        if(null !=spfksj){
//            productTime=Integer.parseInt(spfksj.getMemo());
//        }
//        //获取所有逾期付款订单id key
//        List<String> orderkeys = redisUtil.getLike("order");
//        for (String orderkey:orderkeys
//        ) {
//            String orderId =  redisUtil.get(orderkey).toString();
//            YxAuctionOrder yxAuctionOrder=yxAuctionOrderMapper.selectById(orderId);
//            if(null !=yxAuctionOrder){
//                Calendar instance = Calendar.getInstance();
//                instance.setTime(yxAuctionOrder.getCreateTime());
//                instance.add(Calendar.MINUTE,paimaiTime);
//                Date newCreateTime = instance.getTime();
//                //若为待付款
//                if(0==yxAuctionOrder.getStatus()  && newCreateTime.compareTo(new Date())==-1){
//                    //设为已取消
//                    yxAuctionOrder.setStatus(5);
//                    yxAuctionOrder.setIsDel(1);
//                    YxAuctionRecord yxAuctionRecord=yxAuctionRecordMapper.selectById(yxAuctionOrder.getAuctionRecordId());
//                    if(null !=yxAuctionRecord){
//                        yxAuctionRecord.setIsBuy(0);
//                        redisUtil.set("paimai"+yxAuctionRecord.getId(),yxAuctionRecord.getId());
//                    }
//                    yxAuctionOrderMapper.updateById(yxAuctionOrder);
//                    yxAuctionRecordMapper.updateById(yxAuctionRecord);
//                    redisUtil.del(orderkey);
//                }else if(0==yxAuctionOrder.getStatus()  && new Date().compareTo(newCreateTime)==-1){
//                    DelayOrderWorker delayOrderWorker=new DelayOrderWorker();
//                    DelayOrderQueueManager delayOrderQueueManager=DelayOrderQueueManager.getInstance();
//
//                    delayOrderQueueManager.put(delayOrderWorker,(paimaiTime*60*1000+yxAuctionOrder.getCreateTime().getTime()-System.currentTimeMillis())/1000, TimeUnit.SECONDS);
//                }
//            }
//        }
//
//        //获取所有逾期付款订单id key
//        List<String> productKeys = redisUtil.getLike("product");
//        for (String productKey:productKeys) {
//            String orderId =  redisUtil.get(productKey).toString();
//            YxStoreOrder yxStoreOrder=storeOrderMapper.selectById(orderId);
//            if(null !=yxStoreOrder){
//                Calendar instance = Calendar.getInstance();
//                instance.setTime(yxStoreOrder.getCreateTime());
//                instance.add(Calendar.MINUTE,productTime);
//                Date newCreateTime = instance.getTime();
//                //若为待付款
//                if(0==yxStoreOrder.getStatus()  && 0==yxStoreOrder.getIsDel()  && newCreateTime.compareTo(new Date())==-1){
//                    //设为已取消
//                    storeOrderMapper.updateIsDel(yxStoreOrder.getId());
//                    redisUtil.del(productKey);
//                }else if(0==yxStoreOrder.getStatus()  && 0==yxStoreOrder.getIsDel()  && new Date().compareTo(newCreateTime)==-1){
//                    DelayProductWorker delayProductWorker=new DelayProductWorker();
//                    DelayOrderQueueManager delayOrderQueueManager=DelayOrderQueueManager.getInstance();
//                    delayOrderQueueManager.put(delayProductWorker,(productTime*60*1000+yxStoreOrder.getCreateTime().getTime()-System.currentTimeMillis())/1000, TimeUnit.SECONDS);
//                }
//            }
//        }

        //将所以用户的余额塞入redis中
        List<YxUser> list = userService.list(new QueryWrapper<YxUser>().lambda()
                .orderByAsc(YxUser::getUid));
        list.forEach(user -> {
            redisUtil.set("UserPrice"+user.getUid() , user.getNowMoney());
        });

        //将字典表数据放置redis
        List<YxAppdict> list1 = appdictService.list(new QueryWrapper<YxAppdict>().lambda());
        list1.forEach(dict -> {
            redisUtil.set("dict:"+dict.getCode() , dict.getMemo());
        });
    }


}